/**
 * 
 */
package com.module.osr.ui.stormbringer.charcreator.widgets;

import com.ahq.main.H1HighlightBorder;
import com.dalonedrau.jogl.engine.Action;
import com.dalonedrau.jogl.engine.GameApplet;
import com.dalonedrau.jogl.engine.InputEvent;
import com.dalonedrau.jogl.engine.Interactive;
import com.dalonedrau.jogl.opengl.ui.Button;
import com.dalonedrau.jogl.opengl.ui.GuiComponent;
import com.dalonedrau.jogl.opengl.ui.UIFactory;
import com.dalonedrau.jogl.opengl.ui.panel.CardPanel;
import com.dalonedrau.jogl.opengl.ui.panel.LandscapePagePanel;
import com.dalonedrau.jogl.opengl.ui.panel.Panel;
import com.dalonedrau.jogl.opengl.ui.panel.PortraitPagePanel;
import com.module.osr.stormbringer.bus.SBCharacter;
import com.module.osr.stormbringer.bus.SBPlayer;
import com.module.osr.stormbringer.view.SBCharCreator;
import com.module.stormbringer.globals.Globals;

/**
 * @author Donald
 */
public final class NationalityOptions extends PortraitPagePanel {
	/** the one and only instance of the <code>OverworldView</code> class. */
	private static NationalityOptions	instance;
	/**
	 * Gives access to the singleton instance of {@link NationalityOptions}.
	 * @return {@link NationalityOptions}
	 */
	public static NationalityOptions getInstance() {
		if (NationalityOptions.instance == null) {
			NationalityOptions.instance = new NationalityOptions();
		}
		return NationalityOptions.instance;
	}
	private Button		btnNext;
	private Button		btnPrev;
	private Button[]	btnRaces;
	private int			currentPanel	= 0;
	private CardPanel	pnlRaces;
	/** Creates a new instance of {@link NationalityOptions}. */
	public NationalityOptions() {
		super(Panel.PORTRAIT_RIGHT);
		createButtons();

		pnlRaces = new CardPanel();
		pnlRaces.addChild("0",
				createOptionsPanel(
						btnRaces[Globals.RACE_00MELNIBONE],
						btnRaces[Globals.RACE_01PAN_TANG],
						btnRaces[Globals.RACE_02MYRRHYN],
						btnRaces[Globals.RACE_03DHARIJOR]
									));
		pnlRaces.addChild("1",
				createOptionsPanel(
						btnRaces[Globals.RACE_04JHARKOR],
						btnRaces[Globals.RACE_05SHAZAAR],
						btnRaces[Globals.RACE_06TARKESH],
						btnRaces[Globals.RACE_07VILMIR]
									));
		pnlRaces.addChild("2",
				createOptionsPanel(
						btnRaces[Globals.RACE_08ILMIORA],
						btnRaces[Globals.RACE_09NADSOKOR],
						btnRaces[Globals.RACE_10WEEPING_WASTE],
						btnRaces[Globals.RACE_11ELWHER]
									));
		pnlRaces.addChild("3",
				createOptionsPanel(
						btnRaces[Globals.RACE_12ISLAND_PURPLE],
						btnRaces[Globals.RACE_13ARGIMILIAR],
						btnRaces[Globals.RACE_14PIKARAYD],
						btnRaces[Globals.RACE_15LORMYR]
									));
		pnlRaces.addChild("4",
				createOptionsPanel(
						btnRaces[Globals.RACE_16FILKHAR],
						btnRaces[Globals.RACE_17OIN],
						btnRaces[Globals.RACE_18YU],
						btnRaces[Globals.RACE_19ORG]
									));
		super.addChild(pnlRaces);
		super.addChild(createActionPanel());
		pnlRaces.displayChild("0");
	}
	private Panel createActionPanel() {
		Panel pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		Button btnRandom = UIFactory.getInstance().getStandardButton("Random");
		btnRandom.setHighlightable(true);
		btnRandom.setActive(true);
		btnRandom.setHighlightBorder(new H1HighlightBorder());
		btnRandom.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				try {
					SBCharacter pc =
							(SBCharacter) Interactive.getInstance().getIO(0);
					// set random race
					InputEvent.getInstance().addAction(
							SBPlayer.getInstance(), // object
							SBPlayer.getInstance().getClass().getMethod(
									"setFreshHeroRace",
									new Class[] { SBCharacter.class }),
							new Object[] { pc }
							);
					// SBPlayer.getInstance().setFreshHeroRace(pc);
					// set helper text
					InputEvent.getInstance().addAction(
							SBCharCreator.getInstance(), // object
							SBCharCreator.getInstance().getClass().getMethod(
									"setHelperText",
									new Class[] { Integer.TYPE }),
							new Object[] { SBCharCreator.HELPER_PC_RACE }
							);
					// SBCharCreator.getInstance().setHelperText(
					// Globals.RACE_DESCRIPTIONS[pc.getRace()]);
					// set buttons
					InputEvent.getInstance()
							.addAction(
									NationalityOptions.getInstance(), // object
									NationalityOptions.class.getMethod(
											"setRace", null),
									null
							);
					// setRace(pc.getRace());
				} catch (Exception ex) {
					JOGLErrorHandler.getInstance().fatalError(ex);
				}
			}
		});
		pnl.addChild(btnRandom);

		btnPrev = UIFactory.getInstance().getStandardButton("Prev");
		btnPrev.setHighlightable(true);
		btnPrev.setActive(true);
		btnPrev.setHighlightBorder(new H1HighlightBorder());
		btnPrev.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				try {
					InputEvent.getInstance().addAction(
							NationalityOptions.getInstance(), // object
							NationalityOptions.getInstance().getClass()
									.getMethod(
											"prevPanel",
											null),
							null
							);
				} catch (Exception ex) {
					JOGLErrorHandler.getInstance().fatalError(ex);
				}
			}
		});
		pnl.addChild(btnPrev);

		btnNext = UIFactory.getInstance().getStandardButton("Next");
		btnNext.setHighlightable(true);
		btnNext.setActive(true);
		btnNext.setHighlightBorder(new H1HighlightBorder());
		btnNext.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				try {
					InputEvent.getInstance().addAction(
							NationalityOptions.getInstance(), // object
							NationalityOptions.getInstance().getClass()
									.getMethod(
											"nextPanel",
											null),
							null
							);
				} catch (Exception ex) {
					JOGLErrorHandler.getInstance().fatalError(ex);
				}
			}
		});
		pnl.addChild(btnNext);
		return pnl;
	}
	public void enableButtons(final int choice) {
		for (int i = 0; i < btnRaces.length; i++) {
			if (i == choice) {
				btnRaces[i].setActive(false);
			} else {
				btnRaces[i].setActive(true);
			}
		}
	}
	private void createButtons() {
		btnRaces = new Button[Globals.RACE_DESCRIPTIONS.length];
		for (int i = 0; i < btnRaces.length; i++) {
			btnRaces[i] = new Button("stormbringer_21", Globals.RACE_TITLES[i]);
			btnRaces[i].setHighlightable(true);
			btnRaces[i].setActive(true);
			btnRaces[i].setHighlightBorder(new H1HighlightBorder());
			final int val = i;
			btnRaces[i].addAction(new Action() {
				/**
				 * {@inheritDoc}
				 */
				@Override
				public void performAction() {
					try {
						// enable/disable buttons
						InputEvent.getInstance().addAction(
								NationalityOptions.getInstance(), // object
								NationalityOptions.getInstance().getClass()
										.getMethod(
												"enableButtons",
												new Class[] { Integer.TYPE }),
								new Object[] { val }
								);
						SBCharacter pc =
								(SBCharacter) Interactive.getInstance()
										.getIO(0);
						// set race
						InputEvent.getInstance().addAction(
								SBPlayer.getInstance(), // object
								SBPlayer.getInstance().getClass()
										.getMethod(
												"setFreshHeroRace",
												new Class[] { SBCharacter.class, 
														Integer.TYPE }),
								new Object[] { pc, val }
								);
						//SBPlayer.getInstance().setFreshHeroRace(pc, val);
						// set helper text
						InputEvent.getInstance().addAction(
								SBCharCreator.getInstance(), // object
								SBCharCreator.getInstance().getClass().getMethod(
										"setHelperText",
										new Class[] { Integer.TYPE }),
								new Object[] { SBCharCreator.HELPER_PC_RACE }
								);
						//SBCharCreator.getInstance().setHelperText(
							//	Globals.RACE_DESCRIPTIONS[val]);
					} catch (Exception ex) {
						JOGLErrorHandler.getInstance().fatalError(ex);
					}
				}
			});
		}
	}
	private Panel createOptionsPanel(final Button... btns) {
		Panel pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		pnl.setHorizontalAlignment(GuiComponent.TEXT_JUSTIFY);
		for (int i = 0; i < btns.length; i++) {
			pnl.addChild(btns[i]);
			if (i + 1 < btns.length) {
				pnl.addChild(UIFactory.getInstance().getSpacer(10, 1));
			}
		}
		return pnl;
	}
	public void nextPanel() {
		currentPanel++;
		if (currentPanel >= 4) {
			currentPanel = 4;
			btnNext.setActive(false);
		}
		btnPrev.setActive(true);
		pnlRaces.displayChild(Integer.toString(currentPanel));
	}
	public void prevPanel() {
		currentPanel--;
		if (currentPanel <= 0) {
			currentPanel = 0;
			btnPrev.setActive(false);
		}
		btnNext.setActive(true);
		pnlRaces.displayChild(Integer.toString(currentPanel));
	}
	/**
	 * Sets the nationality assigned through the wizard.
	 */
	public void setRace() throws Exception {
		SBCharacter pc =
				(SBCharacter) Interactive.getInstance().getIO(0);
		int race = pc.getRace();
		for (int i = 0; i < btnRaces.length; i++) {
			if (i == race) {
				btnRaces[i].setActive(false);
			} else {
				btnRaces[i].setActive(true);
			}
		}
		// show panel - 4 races per panel
		currentPanel = race / 4;
		pnlRaces.displayChild(Integer.toString(currentPanel));

		btnNext.setActive(true);
		btnPrev.setActive(true);
		if (currentPanel == 0) {
			btnPrev.setActive(false);
		}
		if (currentPanel == 4) {
			btnNext.setActive(false);
		}
	}
}
